package com.kjsb.controller;

import com.kjsb.entity.Unit;
import com.kjsb.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/units")
public class UnitController {
    @Autowired
    private UnitService unitService;

    /**
     * 信息修改
     * @param unit
     * @return
     */
    @PutMapping
    public Result updateUnits(@RequestBody Unit unit) {
        boolean update = unitService.updateUnits(unit);
        Integer code=update ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg= update ? "修改成功！" : "修改失败，请重试！";
        return new Result(code,update,msg);
    }

    /**
     * 根据unitid删除数据
     * @param unitid
     * @return
     */
    @DeleteMapping("/{unitid}")
    public Result deleteUnits(@PathVariable Integer unitid) {
        boolean i = unitService.deleteUnits(unitid);
        Integer code=i? Code.DELETE_OK : Code.DELETE_ERR;
        String msg= i? "删除成功！" : "删除失败，请重试！";
        return new Result(code,i,msg);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public Result getAllUnits() {
        List<Unit> unitList = unitService.getAllUnits();
        Integer code=unitList != null ? Code.GET_OK : Code.GET_ERR;
        String msg= unitList != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,unitList,msg);

    }

    /**
     * 根据uid查询
     * @param uid
     * @return
     */
    @GetMapping("/{uid}")
    public Result getByUId(@PathVariable Integer uid) {
        Unit i = unitService.getByUId(uid);
        Integer code=i !=null ? Code.GET_OK : Code.GET_ERR;
        String msg=i != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,i,msg);
    }
}
